
import 'dart:typed_data';

import 'package:sqflite/orm/table.dart';

///
abstract class DataClass {

  ///  Convert a value from sqlite table to a value that can be used in Dart.
  static R fromTable<R>(dynamic json) {
    if (json == null) {
      return null as R;
    }

    final typeList = <R>[];

    if (typeList is List<DateTime?>) {
      if (json is int) {
        return DateTime.fromMillisecondsSinceEpoch(json) as R;
      } else {
        return DateTime.parse(json.toString()) as R;
      }
    }

    if (typeList is List<double?> && json is int) {
      return json.toDouble() as R;
    }

    if (typeList is List<bool?> && json is int) {
      return (json != 0) as R;
    }

    // blobs are encoded as a regular json array, so we manually convert that to
    // a Uint8List
    if (typeList is List<Uint8List?> && json is! Uint8List) {
      final asList = (json as List).cast<int>();
      return Uint8List.fromList(asList) as R;
    }

    try {
      return json as R;
    } catch (e) {
      throw ArgumentError('Invalid value for ${typeList.runtimeType}: $json');
    }
  }

  ///  Convert a value to a value that can be stored in sqlite table.
  dynamic toTable<R>(R value) {
    return Table.transferBool(value);
  }
}

///  标记数据库表关联的数据类，不可与表同名
class DataClassName {

  /// Customize the data class name for a given table.
  const DataClassName(this.name);

  /// The overridden name to use when generating the data class for a table.
  final String name;
}

/// 标记需要生成的数据库表
class SqlitexDatabase {

  /// The tables to generate for this database.
  const SqlitexDatabase({required this.tables});

  final List<Type> tables;
}